package com.css.fxfzaqbz.modules.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzaqbz.constants.FxfzConstants;
import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.repository.BigCasualtiesRepository;
import com.css.fxfzaqbz.modules.assess.repository.BigDisasterRepository;
import com.css.fxfzaqbz.modules.assess.repository.BigSecondRepository;
import com.css.fxfzaqbz.modules.assess.repository.EarthEntityRepository;
import com.css.fxfzaqbz.modules.assess2.repository.KrTaskRepository;
import com.css.fxfzaqbz.modules.assess2.repository.UTaskRepository;
import com.css.fxfzaqbz.modules.data.entity.ImportantActivity;
import com.css.fxfzaqbz.modules.data.queryParams.ImportantActivityQueryParams;
import com.css.fxfzaqbz.modules.data.queryParams.SeismicHazardAssessQueryParams;
import com.css.fxfzaqbz.modules.data.repository.ImportantActivityRepository;
import com.css.fxfzaqbz.modules.data.repository.SeismicHazardAssessRepository;
import com.css.fxfzaqbz.modules.data.service.ImportantActivityService;
import com.css.fxfzaqbz.modules.data.service.SeismicHazardAssessService;
import com.css.fxfzaqbz.modules.data.vo.BigStatusVo;
import com.css.fxfzaqbz.util.*;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import com.css.fxfzaqbz.zcpt.sys.service.SysAreaService;
import com.css.fxfzaqbz.zcpt.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service
public class SeismicHazardAssessServiceImpl implements SeismicHazardAssessService {
    @Resource
    SeismicHazardAssessRepository seismicHazardAssessRepository;
    @Resource
    SUserService sUserService;
    @Autowired
    SysAreaService sysAreaService;
    @Resource
    SysRoleService sysRoleService;
    @Resource
    BigSecondRepository bigSecondRepository;
    @Resource
    EarthEntityRepository earthEntityRepository;
    @Resource
    BigDisasterRepository bigDisasterRepository;
    @Resource
    BigCasualtiesRepository bigCasualtiesRepository;
    @Resource
    KrTaskRepository krTaskRepository;
    @Resource
    UTaskRepository uTaskRepository;

    @Override
    public Map<String, Object> getAllSeismicHazardAssess(SeismicHazardAssessQueryParams queryParams) throws UnsupportedEncodingException {

        String userJson = sUserService.getSUser(PlatformSessionUtils.getUserId());
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        String provinceId = sUser.getProvinceId();
        String userId = PlatformSessionContext.getUserID();
        String role = sysRoleService.getRoleByUserId(userId);
        List<Map> maps = JSONObject.parseArray(role, Map.class);//一个人可能有多个角色
        //当前登录人所有的角色
        StringBuilder roles = new StringBuilder();
        for (int i = 0; i < maps.size(); i++) {
            Map map = maps.get(i);
            roles.append(map.get("roleCode") + ",");
        }
        String allRole = roles.toString();

        //如果所有角色包括省级管理员
        if (allRole.contains(FxfzConstants.AQBZ_SJGLY_ROLE)) {

            if (PlatformObjectUtils.isEmpty(sUser.getProvinceId())) {
                return null;
            }
            queryParams.setProvinceId(provinceId);
        }
        //如果所有角色包括国家级管理员
        if (allRole.contains(FxfzConstants.AQBZ_GJJGLY_ROLE)) {
            queryParams.setProvinceId("");
        }
        //如果所有角色包括活动管理员
        if (allRole.contains(FxfzConstants.AQBZ_HDGLY_ROLE)) {
            queryParams.setDlrId(userId);
        }
        //如果所有角色包括业务人员
        if (allRole.contains(FxfzConstants.AQBZ_YERY_ROLE)) {
            return null;
        }
        Map<String, Object> ia = seismicHazardAssessRepository.getAllSeismicHazardAssessParams(queryParams);
        return ia;
    }

    @Override
    public void updateBigStatus(BigStatusVo bigStatusVo) {
        if ("次生灾害隐患评估".equals(bigStatusVo.getContent())) {
            bigSecondRepository.updateBigStatus(bigStatusVo.getBbId());
        }
        if ("地震危险性评估".equals(bigStatusVo.getContent())) {
            earthEntityRepository.updateBigStatus(bigStatusVo.getBbId());
        }
        if ("房屋地震灾害风险评估".equals(bigStatusVo.getContent())) {
            bigDisasterRepository.updateBigStatus(bigStatusVo.getBbId());
        }
        if ("伤亡及需紧急安置人员评估".equals(bigStatusVo.getContent())) {
            bigCasualtiesRepository.updateBigStatus(bigStatusVo.getBbId());
        }
        if ("需紧急安置人员".equals(bigStatusVo.getContent())) {
            uTaskRepository.updateBigStatus(bigStatusVo.getBbId());
        }
        if ("重点场所风险评估".equals(bigStatusVo.getContent())) {
            krTaskRepository.updateBigStatus(bigStatusVo.getBbId());
        }
    }

}
